package com.vinaprime.primeschedule.databaselayer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.vinaprime.primeschedule.entities.VPContact;
import com.vinaprime.primeschedule.entities.VPTaskList;
/** * 
 * @author LONGPN
 * @since 05/11/2013
 *
 */
@SuppressLint("NewApi")
public class VPTaskList_DL {
	//field
	private static final	String	DB_TABLE_NAME	=	"VP_TASKLIST"			;
	private static final	String	VP_TASK_ID	=	"VP_TASK_ID"			;
	private static final	String	VP_TASK_NAME	=	"VP_TASK_NAME"			;
	private static final	String	VP_TASK_CONTENT	=	"VP_TASK_CONTENT"			;
	private static final	String	VP_TASK_START_DATE	=	"VP_TASK_START_DATE"			;
	private static final	String	VP_TASK_FINISH_DATE	=	"VP_TASK_FINISH_DATE"			;
	private static final	String	VP_TASK_ESTIMATE_TIME	=	"VP_TASK_ESTIMATE_TIME"			;
	private static final	String	VP_TASK_ACTUAL_TIME	=	"VP_TASK_ACTUAL_TIME"			;
	private static final	String	VP_TASK_REMIND	=	"VP_TASK_REMIND"			;
	private static final	String	VP_TASK_NOTE	=	"VP_TASK_NOTE"			;
	private static final	String	VP_TASK_ASSIGN	=	"VP_TASK_ASSIGN"			;
	private static final	String	VP_TASK_GROUP	=	"VP_TASK_GROUP"			;
	private static final	String	VP_TASK_IS_ASSIGNED	=	"VP_TASK_IS_ASSIGNED"			;
	private static final	String	VP_TASK_COMPLETE	=	"VP_TASK_COMPLETE"			;
	private static final	String	VP_TASK_PROJECT	=	"VP_TASK_PROJECT"			;
	private static final	String	VP_TASK_FILE	=	"VP_TASK_FILE"			;
	
	public	 DatabaseController_DL	 myDatabaseControllerDL					;
	private static final String DB_DateFormat = "MM/dd/yyyy";
	private DateFormat myDateFormat;
	public VPTaskList_DL(Context c,String path){
		this.myDatabaseControllerDL = new DatabaseController_DL(c,path);
		this.myDateFormat = new SimpleDateFormat(DB_DateFormat);
	}
	
	//Get set
	public static String getDbDateformat() {
		return DB_DateFormat;
	}
	public static String getDbTableName() {
		return DB_TABLE_NAME;
	}
	public static String getVpTaskId() {
		return VP_TASK_ID;
	}
	public static String getVpTaskName() {
		return VP_TASK_NAME;
	}
	public static String getVpTaskContent() {
		return VP_TASK_CONTENT;
	}
	public static String getVpTaskStartDate() {
		return VP_TASK_START_DATE;
	}
	public static String getVpTaskFinishDate() {
		return VP_TASK_FINISH_DATE;
	}
	public static String getVpTaskEstimateTime() {
		return VP_TASK_ESTIMATE_TIME;
	}
	public static String getVpTaskActualTime() {
		return VP_TASK_ACTUAL_TIME;
	}
	public static String getVpTaskRemind() {
		return VP_TASK_REMIND;
	}
	public static String getVpTaskNote() {
		return VP_TASK_NOTE;
	}
	public static String getVpTaskAssign() {
		return VP_TASK_ASSIGN;
	}
	public static String getVpTaskGroup() {
		return VP_TASK_GROUP;
	}
	public static String getVpTaskIsAssigned() {
		return VP_TASK_IS_ASSIGNED;
	}
	public static String getVpTaskComplete() {
		return VP_TASK_COMPLETE;
	}
	public static String getVpTaskProject() {
		return VP_TASK_PROJECT;
	}
	public static String getVpTaskFile() {
		return VP_TASK_FILE;
	}
	//Methods
	public Cursor getAllTaskList()
	{
		
		Cursor cur = null;
		SQLiteDatabase  db = this.myDatabaseControllerDL.getWritableDatabase();
		
		String strSelect = "SELECT * FROM "+ DB_TABLE_NAME;
		cur = db.rawQuery(strSelect, null);		
		return cur;
		
	}
	public Cursor getNewTaskID()
	{
		Cursor cur  = null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT MAX ("+VP_TASK_ID+") AS " +VP_TASK_ID+ " FROM "+ DB_TABLE_NAME;
		cur  = db.rawQuery(strQuery, null);
		return cur;
	}
	public Cursor getTaskbyId(int Id)
	{
		Cursor cur =  null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ VP_TASK_ID +" = " + Id ;
		cur = db.rawQuery(strQuery, null);
		//SQLiteDatabase db = this.myDatabaseControllerDL.getReadableDatabase();
		//String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPC_ID +" = ?" ;
		//cur = db.rawQuery(strQuery, new String[]{String.valueOf(Id)});
		return cur;
	}
	public void AddTask (VPTaskList task)	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put(VP_TASK_ID,  task.getVP_TASK_ID());
		value.put(VP_TASK_NAME,  task.getVP_TASK_NAME());
		value.put(VP_TASK_CONTENT,  task.getVP_TASK_CONTENT());
		
		if (task.getVP_TASK_FINISH_DATE() != null)
        	value.put(VP_TASK_START_DATE, myDateFormat.format(task.getVP_TASK_FINISH_DATE()));
        else
        	value.putNull(VP_TASK_START_DATE);
		
		if (task.getVP_TASK_FINISH_DATE() != null)
        	value.put(VP_TASK_FINISH_DATE, myDateFormat.format(task.getVP_TASK_FINISH_DATE()));
        else
        	value.putNull(VP_TASK_FINISH_DATE);
		
		value.put(VP_TASK_ESTIMATE_TIME,  task.getVP_TASK_ESTIMATE_TIME());
		value.put(VP_TASK_ACTUAL_TIME,  task.getVP_TASK_ACTUAL_TIME());
		value.put(VP_TASK_REMIND,  task.getVP_TASK_REMIND());
		value.put(VP_TASK_NOTE,  task.getVP_TASK_NOTE());
		value.put(VP_TASK_ASSIGN,  task.getVP_TASK_ASSIGN());
		value.put(VP_TASK_GROUP,  task.getVP_TASK_GROUP());
		value.put(VP_TASK_IS_ASSIGNED,  task.getVP_TASK_IS_ASSIGNED());
		value.put(VP_TASK_COMPLETE,  task.getVP_TASK_COMPLETE());
		value.put(VP_TASK_PROJECT,  task.getVP_TASK_PROJECT());
		value.put(VP_TASK_FILE,  task.getVP_TASK_FILE());
		
		db.insert(DB_TABLE_NAME, null, value);		
		db.close();
	}
	public int updateTask (VPTaskList task)
	{
		SQLiteDatabase db = this.myDatabaseControllerDL .getWritableDatabase();
		ContentValues value = new ContentValues();
		value.put(VP_TASK_ID,  task.getVP_TASK_ID());
		value.put(VP_TASK_NAME,  task.getVP_TASK_NAME());
		value.put(VP_TASK_CONTENT,  task.getVP_TASK_CONTENT());
		
		if (task.getVP_TASK_FINISH_DATE() != null)
        	value.put(VP_TASK_START_DATE, myDateFormat.format(task.getVP_TASK_FINISH_DATE()));
        else
        	value.putNull(VP_TASK_START_DATE);
		
		if (task.getVP_TASK_FINISH_DATE() != null)
        	value.put(VP_TASK_FINISH_DATE, myDateFormat.format(task.getVP_TASK_FINISH_DATE()));
        else
        	value.putNull(VP_TASK_FINISH_DATE);
		
		value.put(VP_TASK_ESTIMATE_TIME,  task.getVP_TASK_ESTIMATE_TIME());
		value.put(VP_TASK_ACTUAL_TIME,  task.getVP_TASK_ACTUAL_TIME());
		value.put(VP_TASK_REMIND,  task.getVP_TASK_REMIND());
		value.put(VP_TASK_NOTE,  task.getVP_TASK_NOTE());
		value.put(VP_TASK_ASSIGN,  task.getVP_TASK_ASSIGN());
		value.put(VP_TASK_GROUP,  task.getVP_TASK_GROUP());
		value.put(VP_TASK_IS_ASSIGNED,  task.getVP_TASK_IS_ASSIGNED());
		value.put(VP_TASK_COMPLETE,  task.getVP_TASK_COMPLETE());
		value.put(VP_TASK_PROJECT,  task.getVP_TASK_PROJECT());
		value.put(VP_TASK_FILE,  task.getVP_TASK_FILE());		
		return db.update(DB_TABLE_NAME, value, VP_TASK_ID + "= ?", new String[] {String.valueOf(task.getVP_TASK_ID())});
		
	}
	public void deleteTask (VPTaskList task)
	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, VP_TASK_ID + "= ?", new String[] { String.valueOf(task.getVP_TASK_ID()) });
		db.close();
	}
	public void deleteTaskID(int task_id){
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, VP_TASK_ID + "= ?",new String[]{ String.valueOf(task_id)});
		db.close();
	}
}
